package de.guice.aktenschrank.crud;

import java.sql.Connection;
import java.sql.DriverManager;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

import de.guice.aktenschrank.configuration.Annotation.Trace;

@Singleton
public class ConnectionManagerJDBCImpl implements ConnectionManager {

	Connection connection;
	
	@Inject
	@Named("JDBC USER")
	String username;

	@Inject
	@Named("JDBC PASSWORD")
	String password;
	
	@Inject
	@Named("JDBC URL")
	String url;
	
	@Override
	@Trace("Establishing Datebase Connection")
	public Connection getConnection() throws Throwable {
			
		if (connection == null) {

			try {
				Class.forName("com.mysql.jdbc.Driver").newInstance();
				connection = DriverManager.getConnection(this.url, this.username,this.password);
			} catch (Exception e) {
				throw new Throwable("Error creating Database Connection: " + e.getMessage());
			}
			
		}

		return connection;

	}
}
